﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Img.Service;
using Crawl;
using System.IO;

namespace Img
{
    public partial class FrmImg : Form
    {
        private bool isCrawling;
        private static bool isReadHis;
        public static bool IsReadHis { get { return isReadHis; } }
        private static bool isUseProxy;
        public static bool IsUseProxy { get { return isUseProxy; } }
        private static int imgCrawlerNum = 4;
        public static int ImgCrawlerNum { get { return imgCrawlerNum; } }
        public FrmImg()
        {
            InitializeComponent();           
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);            
        }

        #region Log
        private void Log(string msg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() =>
                {
                    this.PrintLn(msg);
                }));
            }
            else
            {
                this.PrintLn(msg);
            }
        }
        private void PrintLn(string msg)
        {
            string[] lines = this.messageText.Lines;
            if (lines.Length >= 100)
            {
                messageText.Text = "";
            }
            messageText.AppendText(msg + Environment.NewLine);
            messageText.ScrollToCaret();
        }
        #endregion

        private void startBtn_Click(object sender, EventArgs e)
        {
            
            if (isCrawling == true)
            {
                this.Log("爬虫已启动");
                return;
            }            
          
            CrawlService crawlServ = CrawlService.Instance;
            if (!crawlServ.Init(new CrawlService.Log(Log)))
            {
                this.Log("爬虫服务初始化失败");
                return;
            }

            string[] urls = readFile(txtUrlFile.Text.Trim());
            if (urls == null || urls.Length < 1) 
            {
                return;
            }
            CrawlerEvents crawlerEvents = new CrawlerEvents(new OnStart(OnCrawlStart), new OnCrawl(OnCrawlUrl), new OnStop(OnCrawlStop));
            
            if (!crawlServ.Start(txtRefer.Text, txtUrlFile.Text.Trim(),crawlerEvents))
            {
                this.Log("爬虫服务启动失败");
                return;     
            }
            this.Log("爬虫服务启动成功");
            isCrawling = true;                
        }
 
        private void OnCrawlStart(Crawler crawler)
        {
            this.Log(crawler.Name + "启动~~");
        }

        private void OnCrawlStop(Crawler crawler)
        {
            this.Log(crawler.Name + "结束~~");
        }

        private void OnCrawlUrl(Crawler crawler, string url)
        {
            this.Log(url);

        }

        private void stopBtn_Click(object sender, EventArgs e)
        {
            if (isCrawling == false)
            {
                this.Log("爬虫还没启动");
                return;
            }
            CrawlService.Instance.Destroy();   
            isCrawling = false;
        }

        private void chkIsReadHis_CheckedChanged(object sender, EventArgs e)
        {
            isReadHis = chkIsReadHis.Checked;
        }

        private void chkUserProxy_CheckedChanged(object sender, EventArgs e)
        {
            isUseProxy = chkUserProxy.Checked;
        }

        private void FrmImg_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确定要退出图片爬虫", "提示", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                e.Cancel = true;
                return;
            }
            else
            {
                if (isCrawling)
                {
                    this.stopBtn_Click(sender, new EventArgs());
                }
            }
        }
        private void domainCrawlerNum_TextChanged(object sender, EventArgs e)
        {
            imgCrawlerNum = Int32.Parse(domainCrawlerNum.Text);
        }

        private void btnOpenDir_Click(object sender, EventArgs e)
        {
            string path = "jobs\\output";
            DirectoryInfo dir = new DirectoryInfo(path);
            if (!dir.Exists) 
            {
                dir.Create();
            }
            System.Diagnostics.Process.Start("explorer.exe", path);
        }

        private void btnOpenTxt_Click(object sender, EventArgs e)
        {
            string path = txtUrlFile.Text;
            FileInfo fileInfo = new FileInfo(path);
            if (!fileInfo.Exists) 
            {
                MessageBox.Show("文件不存在:"+ fileInfo.FullName);
            }
            System.Diagnostics.Process.Start("explorer.exe", path);
        }

        private string[] readFile(string fileName) 
        {
            if (!File.Exists(fileName))
            {
                Log("链接文件不存在");
                return new string[0];
            }
            Log("开始加载链接种子");
            string[] urlLines = File.ReadAllLines(fileName);
            if (urlLines.Length < 1)
            {
                Log("文件为空!!!");                
            }
            return urlLines;
        }
    }
    
}
